from core import Request
from core.tao import HttpHeader
from core.util import CodeUtil
from core.Func import TypeOf

sid = None

'''session 登录'''
def Login(username, password):
    pass


'''get方式获取数据'''
def Get(url, params=None, retType="json"):
    global sid
    if params is None:
        params = {}
    if sid is not None:
        params['sid'] = sid
    resp = Request.Post(url, params)

    retData = {'body': None, 'headers': resp.headers}
    if retType == 'json':
        try:
            retData['body'] = CodeUtil.JSONDecode(resp.content.decode('utf-8'))
        except Exception as e:
            raise '返回的不是json数据'
    elif retType == 'file':
        if HttpHeader.IsAttachment(retData.headers):
            retData['body'] = None
        else:
            raise '返回的不是文件'
        pass
    else:
        pass
    return retData


'''post方式获取数据'''
def Post(url, params=None, retType="json"):
    global sid
    if params is None:
        params = {}
    if sid is not None:
        params['sid'] = sid
    resp = Request.Post(url, params)

    retData = {'body': None, 'headers': resp.headers}
    if retType == 'json':
        try:
            retData['body'] = CodeUtil.JSONDecode(resp.content.decode('utf-8'))
        except Exception as e:
            raise '返回的不是json格式数据'
    elif retType == 'file':
        if HttpHeader.IsAttachment(retData.headers):
            retData['body'] = None
        else:
            raise '返回的不是文件'
        pass
    else:
        pass
    return retData

def Describe(message: str, fn, ignore: bool = False):
    if ignore is True:
        return
    if TypeOf(fn) != 'function':
        raise '测试函数错误'
    fn()

def It(message: str, fn, ignore: bool = False):
    if ignore is True:
        return
    if TypeOf(fn) != 'function':
        raise '测试函数错误'
    fn()
